/*
    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) 2010  <name of author>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "TextureDescription.h"
#include "TexDescException.h"

#include "File.h"

#include <sstream>
#include <string>
#include <set>

using namespace std;

TextureDescription::TextureDescription(const std::string& iTextureFileName)
{
   File file(iTextureFileName);

   string line;

   while(!file.isAtTheEnd())
   {
      line = file.readLine();
      istringstream is(line);

      while(!is.eof())
      {
         sprite newSprite;
         is >> newSprite._id;
         if(is.eof()) //If we have reached the end, the first read will break ! Must do that because STL is strange with VC10. 
            break;
         is >> newSprite._bl.x;
         is >> newSprite._bl.y;
         is >> newSprite._tl.x;
         is >> newSprite._tl.y;
         is >> newSprite._tr.x;
         is >> newSprite._tr.y;
         is >> newSprite._br.x;
         is >> newSprite._br.y;

         _sprites.insert(newSprite);
      }
   }
}


TextureDescription::~TextureDescription()
{
   
}

void TextureDescription::getTexCoord(const unsigned int& iID, float2& oBL, float2& oTL, float2& oTR, float2& oBR) const
{
   sprite spriteToFind;
   spriteToFind._id = iID;
   set<sprite>::const_iterator itElem = _sprites.find(spriteToFind);

   //Check if we find the value
   if(itElem == _sprites.end())
      throw new TexDescException();

   //We have the element => fill output
   oBL = itElem->_bl;
   oTL = itElem->_tl;
   oTR = itElem->_tr;
   oBR = itElem->_br;
}

